package view

import (
    "data"
    "errors"
    "strconv"
    "helper/response"
)

type UserKill struct {
    ca      *data.Cache
}


func NewUserKill() *UserKill {
    uk := new(UserKill)
    if uk == nil {
        return nil
    }

    var ca = data.NewCache()
    if ca == nil {
        return nil
    }
    uk.ca = ca
    return uk
}

func (uk *UserKill) UserSecKill(userid, productid string) (string, error) {
    if productid == "" || userid == ""{
        err := errors.New("userkill: params error")
        responseStr, _ := response.UserSecKillInfoResponse(0, 2, -1)
        return responseStr, err
    }
    goodsid, err := uk.ca.GetUserKill(userid, productid)
    if err != nil {
        responseStr, _ := response.UserSecKillInfoResponse(0, 2, -1)
        return responseStr, err
    } else {
        goodsidInt, err := strconv.Atoi(goodsid)
        if err != nil {
            responseStr, _ := response.UserSecKillInfoResponse(0, 2, -1)
            return responseStr, err
        } else {
            responseStr, _ := response.UserSecKillInfoResponse(0, 2, goodsidInt)
            return responseStr, nil
        }
    }
}
